package org.example.blog_system.service;

import org.example.blog_system.mapper.UserMapper;
import org.example.blog_system.model.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;
    // session里的验证码 的key
    private static final String   ADMIN_KAPTCHA_SESSION_KEY = "ADMIN_KAPTCHA_SESSION_KEY";
    // 验证码创建时间 的key
    private static final String ADMIN_KAPTCHA_SESSION_DATE =  "ADMIN_KAPTCHA_SESSION_DATE";
    // 验证码的 最大存在时间 , 以毫秒为单位 :60秒
    private static final  long TIME_OUT = 60 * 1000 ;

    //判断username和password 是否正确  用户名或密码错误输出 0 , 验证码错误输出1 , 都正确返回2
    public Integer checkUser(String username , String password , String inputCaptcha , HttpSession session){

        if(!StringUtils.hasLength(username) || !StringUtils.hasLength(password)){
            return 0 ;
        }
        String captcha = (String) session.getAttribute(ADMIN_KAPTCHA_SESSION_KEY);
        Date captchaDate = (Date) session.getAttribute(ADMIN_KAPTCHA_SESSION_DATE);
        // 判断验证码是否正确, 并且验证码时间是否超时
        if(!StringUtils.hasLength(inputCaptcha) || !captcha.equalsIgnoreCase(inputCaptcha) ||
                (System.currentTimeMillis()-captchaDate.getTime()) > TIME_OUT){
            return 1 ;
        }
        UserInfo userInfo = userMapper.getUserByName(username);
        if(userInfo == null){
            return  0;
        }

        if(userInfo.getPassword().equals(password) ){
            // 将 user 加进session
            session.setAttribute("userInfo" ,userInfo);
            return 2 ;
        }
        return 0 ;
    }

    public UserInfo getUserInfo(HttpSession session , Integer blogId) {

        UserInfo userInfo = (UserInfo) session.getAttribute("userInfo");
        if(blogId == null){
             UserInfo userInfo1 = new UserInfo();
             userInfo1.setUsername(userInfo.getUsername());
            return userInfo1 ;
        }else{
            UserInfo userInfo2 = userMapper.getUserByBlogId(blogId);

            return  userInfo2 ;
        }
    }

    public boolean outUser(HttpSession session) {
        if (session != null){
            session.removeAttribute("userInfo");
        }
        return true;
    }

    public boolean isUser(HttpSession session, Integer blogId) {
        if(session == null || session.getAttribute("userInfo")==null){
            return false ;
        }

        UserInfo userInfo = (UserInfo) session.getAttribute("userInfo");
        UserInfo userInfo2 = userMapper.getUserByBlogId(blogId);
        if(userInfo2!= null && userInfo.getUsername().equals(userInfo2.getUsername())){
            return true ;
        }
        return false ;
    }

    public boolean createUser(HttpSession session,UserInfo userInfo, String againPassword) {
          if(!StringUtils.hasLength(userInfo.getUsername()) || !StringUtils.hasLength(userInfo.getPassword())||!StringUtils.hasLength(againPassword)){
              return false ;
          }
          if(!userInfo.getPassword().equals(againPassword)){
              return false ;
          }
           Integer update = userMapper.createUser(userInfo);
          if(update != 0){
              UserInfo userInfo2 = new UserInfo();
              userInfo2.setUserid(userInfo.getUserid());
              userInfo2.setUsername(userInfo.getUsername());
              userInfo2.setPassword(userInfo.getPassword());
              session.setAttribute("userInfo" , userInfo2);
              return true ;
          }

        return  false ;
    }
}
